import lioncen.cti.jmsi.*;
import lioncen.cti.jmsi.object.*;
import lioncen.cti.jmsi.engine.*;
import javax.servlet.http.Cookie
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import cn.umum.msi.listener.MSIAdapter
import cn.umum.common.BaseFunction as BF;
//import com.sun.enterprise.web.connector.grizzly.comet.CometContext;
//import com.sun.enterprise.web.connector.grizzly.comet.CometEngine;
//import com.sun.enterprise.web.connector.grizzly.comet.CometEvent;
//import com.sun.enterprise.web.connector.grizzly.comet.CometHandler;


class MsiController {
    def index = {
        def password="iWuiSdHR0dErH6f%2FX%2Fhnu1fsG01SmLyy";
        password=URLDecoder.decode(password, 'UTF-8');
        render("<br>encodeaa=")
        render(password);
        render("<br>decode=")
        //render (BF.AESDecryptCtr(password,"ymcookies",256))
        //return;
        Cookie msicookiepassword = new Cookie("userpassword","RXKiSQgICAh7VUcat1cYazTjGi1xMGJd")
        msicookiepassword.setMaxAge(60*60*24*30);
        msicookiepassword.setPath("/msi/");
        response.addCookie(msicookiepassword)
        render(msicookiepassword)
        Cookie msicookieuserid = new Cookie("userid","1513")
        msicookieuserid.setMaxAge(60*60*24*30);
        msicookieuserid.setPath("/msi/");
        response.addCookie(msicookieuserid)
        render(msicookieuserid)
        redirect(uri: '/msi/userinfo?domain=umum')
        //msi.setAgentStatus(2)
    }
    def userinfo={ MsiLoginCommand cmd ->
        def msiinfo;
        if (cmd.hasErrors()) {
            flash.message = "登录错误，请重新输入"
            Success=false;
        } else {


            def childuser=User.findByIdAndTruepassword(cookie(name:"userid"),BF.AESDecryptCtr(URLDecoder.decode(cookie(name:"userpassword")),"ymcookies",256))
            if(childuser==null){render "error childuser"+BF.AESDecryptCtr(cookie(name:"userpassword"),"ymcookies",256);return}
            def parentuser=User.findById(childuser.userParentid)
            if(parentuser==null){render "error parentuser";return}
            def domain=UserDomain.findByDomain(params.domain)
            if(domain==null){render "error domain";return}
            def companysn = Companysn.findByUserAndDomain(parentuser,domain);
            if(companysn==null){render "error companysn";return}
            msiinfo = companysn ? MsiInfo.findByCompanySNAndUser(companysn,childuser) : null;
            
            if(msiinfo==null){render "error msiinfo";return}
            private int agentID, ctiPort;
            agentID=Integer.valueOf(companysn.companySN+msiinfo.agentID);
            public static MSI msi = null;
            msi = servletContext.getAttribute("msi"+cookie(name:"MSI-JSESSIONID"));
            if(msi==null){
                msi = new MSI();
                msi.ctiEventListener = new MSIAdapter(agentID);
                private String agentDN,ctiIP,accdns;
                private boolean	isMonitor,isSilentMode
                ctiIP = "222.42.3.28";
                ctiPort = 10089;
                msi.setConnInfo(ctiIP,ctiPort);
                msi.start();
                Cookie namecookie = new Cookie("MSI-JSESSIONID",cookie(name:'JSESSIONID'))
                namecookie.setMaxAge(60*60*24*30);
                namecookie.setPath("/msi/");
                response.addCookie(namecookie)
                servletContext.setAttribute("msi"+cookie(name:"JSESSIONID"),msi)
                flash.message = "连接msi成功"
            }else{
                msi.ctiEventListener = new MSIAdapter(agentID);
                /*
                Cookie msicookiepassword = new Cookie("password","pW6bSVFRUVFiNrmcSwMiKVYSLXiC3s7p")
                msicookiepassword.setMaxAge(60*60*24*30);
                msicookiepassword.setPath("/msi/");
                response.addCookie(msicookiepassword)
                render(msicookiepassword)
                Cookie msicookieuserid = new Cookie("userid","1513")
                msicookieuserid.setMaxAge(60*60*24*30);
                msicookieuserid.setPath("/msi/");
                response.addCookie(msicookieuserid)
                render(msicookieuserid)
                render(BaseFunction.AESEncryptCtr("test","ymcookies",256))
                render (BaseFunction.AESDecryptCtr("6oWfScXFxcVfb6GGTXoqCzbuDGphSKhQ","ymcookies",256))
                 */
               
            }
        }
        //flash.message=cookie(name:"JSESSIONID");
        
        render(view:'_userinfo',model:[BF:BF,msiinfo:msiinfo])
        // }
    }
    def userinfo2={
        def fields="a,b,c,d,e,f";
        fieldsList = fields.split(',');
    }
    def fieldsList=[];

}
